"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 设置系统时区为具有夏令时时区，使用gs_basebackup备份数据库
Create At   : 2020/11/16
@zou_jialiang0505328126
Description :
    1.设置夏令时
    2.主节点执行备份指令
    3.使用备份文件恢复数据库
    4.重启集群
    5.若备节点状态异常对备库进行build
    6.查询数据是否与备份时一致
Expect      :
    1.设置成功
    2.备份成功
    3.恢复成功
    4.重启集群成功，可能提示集群状态为degrade
    5.备机重建成功
    6.数据一致
History     :
    Modified by @wan005 2021/1/21: 修改gs_basebackup断言，增加断言信息
    Modified by @wan005 2021/3/23: 增加环境时区修复步骤
    Modified by @wan005 2021/3/26: 调整wal_sender_timeout,规避因环境性能差导致的失败
"""

import datetime
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

New_York_Local = '/usr/share/zoneinfo/America/New_York'
LN_SF_CMD = 'ln -sf {zone} /etc/localtime'


class Basebackup(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '----Opengauss_Reliability_Tools_Basebackup_Case017.py 开始执行----')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta2_dbuser = Node(node='Standby2DbUser')
        self.sta1_root = Node(node='Standby1Root')
        self.pri_root = Node(node='PrimaryRoot')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.constant = Constant()
        self.common = Common('PrimaryRoot')
        self.backup_path = os.path.join(macro.DB_BACKUP_PATH, 'base_backup')

        result = self.common.check_system_timezone(
            *('PrimaryRoot', 'Standby1Root', 'Standby2Root'))
        self.log.info(result)
        self.assertTrue(result)

        initial_time_cmd = 'date \'+%Y-%m-%d %H:%M:%S\''
        self.initial_time = self.pri_root.sh(initial_time_cmd).result()
        self.initial_zone = self.pri_root.sh('ls -l /etc/localtime').result()
        init_info = f'initial info: {self.initial_time}, {self.initial_zone}.'
        self.log.info(init_info)

        result = self.pri_sh.executDbSql('show wal_sender_timeout;')
        self.log.info(f"wal_sender_timeout is {result}")
        self.wal_sender_timeout = result.strip().splitlines()[-2]

    def test_basebackup(self):
        self.log.info('-----------修改wal_sender_timeout------------------')
        result = self.pri_sh.executeGsguc('reload',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          'wal_sender_timeout=12s')
        self.assertTrue(result)

        self.log.info('----修改 postgresql.conf设置timezone----')
        self.initial_conf = os.path.join(macro.DB_INSTANCE_PATH,
                                         'postgresql.conf')
        self.new_conf = os.path.join(macro.DB_INSTANCE_PATH,
                                     'postgresql.conf.new')
        set_timezone_cmd = f"cp {self.initial_conf} {self.new_conf}; " \
            f"echo 'timezone=America/New_York' >> {self.initial_conf}"
        self.log.info(set_timezone_cmd)
        self.pri_dbuser.sh(set_timezone_cmd)

        self.log.info('----重载配置文件使配置文件生效----')
        result = self.pri_sh.reloadDbConfig()
        self.assertIn(self.constant.RELOAD_SUCCESS_MSG, result)

        self.log.info('----设置主节点时区为纽约----')
        set_sys_timezone = LN_SF_CMD.format(zone=New_York_Local)
        self.log.info(set_sys_timezone)
        set_result = self.pri_root.sh(set_sys_timezone).result()
        self.log.info(set_result)
        self.log.info('----设置操作系统时间:03/08/2020 01:59:50(3月第二个周日，即将开始夏令时)----')
        datecmd = "date -s '03/08/2020 01:59:50';hwclock --systohc;" \
                  "service ntpd stop;ntpdate ntp.api.bz;service ntpd start"
        self.log.info(datecmd)
        self.log.info(self.pri_root.sh(datecmd).result())

        self.log.info('----查看当前时区----')
        result = self.pri_root.sh('ls -l /etc/localtime').result()
        self.log.info(result)
        self.log.info('----查看当前数据库系统时间----')
        before_time = self.pri_sh.executDbSql('select now();show timezone;')
        self.log.info(before_time)
        self.assertTrue('2020-03-08 01:59' in before_time
                        or '2020-03-08 03' in before_time)

        self.log.info('----创建备份路径----')
        shell_cmd = f'rm -rf {self.backup_path}; mkdir {self.backup_path}'
        self.log.info(shell_cmd)
        result = self.pri_dbuser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----开始备份----')
        result = self.pri_sh.execu_gs_basebackup(self.backup_path,
                                                 self.pri_dbuser.db_host,
                                                 self.pri_dbuser.db_port, )
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result)
        self.assertNotIn('error', result)
        self.assertIn(self.constant.BASEBACKUP_SUCCESS_MSG, result)
        result = self.pri_dbuser.sh(
            f'ls -l {self.backup_path} | grep "^-" | wc -l').result()
        self.log.info(result)
        self.assertLess(0, int(result))
        time.sleep(10)

        self.log.info('----查看数据库当前时间----')
        afterTime = self.pri_sh.executDbSql('select now();show timezone;')
        self.log.info(afterTime)
        self.assertIn('2020-03-08 03', afterTime)
        self.log.info(self.pri_root.sh('date').result())

        self.log.info('----使用备份文件恢复数据库----')
        result = self.pri_sh.stopDbCluster()
        self.assertTrue(result)
        shell_cmd = f'rm -rf  {macro.DB_INSTANCE_PATH}/* ;' \
            f'cp -R {self.backup_path}/* {macro.DB_INSTANCE_PATH}'
        self.log.info(shell_cmd)
        result = self.pri_dbuser.sh(shell_cmd).result()
        self.log.info(result)
        time.sleep(2)

        self.log.info('----启动集群，启动数据库成功或出现因为备机为Need repair状态的失败----')
        result = self.pri_sh.startDbCluster('get_detail')
        self.log.info(result)
        result = ('Degraded' in result) or (
                self.constant.START_SUCCESS_MSG in result)
        self.assertTrue(result)
        time.sleep(2)

        self.log.info('----检查备机是否需要重建----')
        result = self.pri_sh.check_whether_need_build()
        success_msg = self.constant.BUILD_SUCCESS_MSG
        if result:
            result = self.sta_1_sh.executeGsctl('build',
                                                success_msg,
                                                '-b full')
            self.assertTrue((result))
            result = self.sta_2_sh.executeGsctl('build',
                                                success_msg,
                                                '-b full')
            self.assertTrue((result))

        self.log.info('----检查主备是否同步----')
        result = self.sta_1_sh.check_data_consistency()
        self.assertTrue(result)
        result = self.sta_2_sh.check_data_consistency()
        self.assertTrue(result)

        self.log.info('----检查数据一致性----')
        nodes_tuple = (self.pri_dbuser, self.sta1_dbuser, self.sta2_dbuser)
        flag = self.common.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag)

        table_dict = self.common.format_sql_result(
            self.pri_sh.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.common.check_data_sample_by_all(select_sql,
                                                        *nodes_tuple)
            self.assertTrue(flag)

    def tearDown(self):
        self.log.info('----环境清理----')
        self.log.info('----删除备份文件----')
        shell_cmd = f'rm -rf {self.backup_path}'
        self.log.info(shell_cmd)
        result = self.pri_dbuser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----恢复数据库时区设置----')
        cmd = f'mv {self.new_conf} {self.initial_conf};'
        self.pri_dbuser.sh(cmd)
        result = self.pri_sh.reloadDbConfig()
        self.log.info(result)

        self.log.info('----恢复操作系统时区设置----')
        result = self.pri_root.sh('ls -l /etc/localtime').result()
        self.log.info(result)
        recover_timezone = self.initial_zone.split('->')[1]
        self.log.info(recover_timezone)
        recovery_zone_cmd = LN_SF_CMD.format(zone=recover_timezone)
        self.log.info('recover: ' + recovery_zone_cmd)
        self.pri_root.sh(recovery_zone_cmd)
        self.log.info('----查看当前时区----')
        result = self.pri_root.sh('ls -l /etc/localtime').result()
        self.log.info(result)

        self.log.info('----恢复操作系统时间----')
        currentTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        date_cmd = f'date -s \'{currentTime}\''
        self.log.info('recover: ' + date_cmd)
        self.log.info(self.pri_root.sh(date_cmd).result())
        self.log.info(self.pri_root.sh('date').result())
        self.log.info('----查看当前数据库系统时间----')
        beforeTime = self.pri_sh.executDbSql('select now();show timezone;')
        self.log.info(beforeTime)

        self.pri_sh.getDbClusterStatus('status')

        self.log.info('----检查备机是否需要重建----')
        result = self.pri_sh.check_whether_need_build()
        if result:
            self.sta_1_sh.executeGsctl('build',
                                       self.constant.BUILD_SUCCESS_MSG,
                                       '-b full')
            self.sta_2_sh.executeGsctl('build',
                                       self.constant.BUILD_SUCCESS_MSG,
                                       '-b full')

        self.log.info('-----------还原wal_sender_timeout------------------')
        result = self.pri_sh.executeGsguc('reload',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f'wal_sender_timeout='
                                          f'{self.wal_sender_timeout}')
        self.log.info(result)
        self.log.info(
            '----Opengauss_Reliability_Tools_Basebackup_Case017.py 执行结束----')
